package com.cooper.scraper;

import android.util.Log;

public class DownloadThread extends Thread {

	protected DownloadParams params;
	
	protected HttpDownload httpDownload;
	
	public DownloadThread(DownloadParams params) {
		super();
		this.params = params;
		httpDownload = new HttpDownload(params);
	}
	
	protected void complete(String key, Object result) {
		if (!Thread.currentThread().isInterrupted()) {
			Log.d("DownloadThread", "Download is complete:" + key);
			params.putResult(key, result);
			params.getHandler().sendEmptyMessage(
					Downloader.MESSAGE_DOWNLOAD_COMPLETE);
		} else {
			Log.d("DownloadThread", "Download is Interrupted" + key);
			params.putCancel(key);
			params.getHandler().sendEmptyMessage(
					Downloader.MESSAGE_DOWNLOAD_CANCEL);
		}
	}

	protected void fail(String key, int errorCode) {
		if (!Thread.currentThread().isInterrupted()) {
			Log.d("DownloadThread", "Download fail" + key);
			params.putFail(key, errorCode);
			params.getHandler().sendEmptyMessage(
					Downloader.MESSAGE_DOWNLOAD_FAIL);
		} else {
			Log.d("DownloadThread", "Download is Interrupted" + key);
			params.putCancel(key);
			params.getHandler().sendEmptyMessage(
					Downloader.MESSAGE_DOWNLOAD_CANCEL);
		}
	}
}
